﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 18.03.2008
 * Zeit: 15:29
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Xml;
using System.Drawing;

namespace Gamadus.Designer
{
	public enum EventType
	{
		None,
		Step,
		Create,
		Draw
	}
	public class Object : Resource
	{
		private List<Event> events = new List<Event>();
		
		public Object(int id):base(id)
		{
			this.type = ResourceType.Object;
			Settings["visible"] = true;
			Settings["solid"] = false;
			Settings["persistent"] = false;
			Settings["depth"] = 0;
		}
		
		protected override IEditWindow showEditForm()
		{
			return new EditObjectForm(this);
		}
		
		public Event AddEvent(EventType type)
		{
			Event evt = new Event(type);
			events.Add(evt);
			return evt;
		}
		
		public List<Event> CloneEvents()
		{
			List<Event> list = new List<Event>();
			foreach(Event evt in events)
			{
				list.Add(evt.Clone());
			}
			return list;
		}
		
		public void SaveEvents(List<Event> list)
		{
			events = list;
		}
		public override void ReadXml(XmlElement element)
		{
			if(element.GetElementsByTagName("Events").Count == 0)
				return;
			
			XmlElement eventChild = (XmlElement)element.GetElementsByTagName("Events")[0];
			foreach(XmlElement child in eventChild.ChildNodes)
			{
				if(!child.HasAttribute("Type") || child.ChildNodes.Count == 0)
					continue;
				
				EventType type = (EventType)Enum.Parse(typeof(EventType), child.Attributes["Type"].InnerText);
				Event evt = AddEvent(type);
				foreach(XmlElement actionChild in child.ChildNodes)
				{
					if(actionChild.Name != "Action" || !actionChild.HasAttribute("Name"))
						continue;
					
					ActionPrototype prototype = ActionSetMgr.Instance.FindPrototypeByName(actionChild.Attributes["Name"].InnerText);
					if(prototype == null)
						continue;
					
					Action act = new Action(prototype);
					evt.Actions.Add(act);
					foreach(XmlElement paramChild in actionChild.ChildNodes)
					{
						if(paramChild.Name != "Param" || !paramChild.HasAttribute("Name") || !paramChild.HasAttribute("Type"))
							continue;
						
						ActionParam param = act.ParamList[paramChild.Attributes["Name"].InnerText];
	
						if(param.GetType().ToString() != "Gamadus.Designer."+paramChild.Attributes["Type"].InnerText)
							continue;
						
						
						param.ReadXml(paramChild);
					}
				}
			}
		}
		
		public override void WriteXml(XmlTextWriter xml)
		{
			xml.WriteStartElement("Events");
			foreach(Event evt in events)
			{
				xml.WriteStartElement("Event");
				xml.WriteAttributeString("Type", evt.Type.ToString());
				foreach(Action act in evt.Actions)
				{
					xml.WriteStartElement("Action");
					xml.WriteAttributeString("Name", act.Prototype.Name);
					foreach(KeyValuePair<string, ActionParam> pair in act.ParamList)
					{
						xml.WriteStartElement("Param");
						xml.WriteAttributeString("Name", pair.Key);
						xml.WriteAttributeString("Type", pair.Value.GetType().ToString().Substring(17));
						pair.Value.WriteXml(xml);
						xml.WriteEndElement();
					}
					xml.WriteEndElement();
				}
				xml.WriteEndElement();
			}
			xml.WriteEndElement();
		}
	}
	
	public class Event
	{
		public EventType Type;
		public List<Action> Actions = new List<Action>();
		
		public string Name
		{
			get
			{
				return Type.ToString();
			}
		}
		
		public Event(EventType type)
		{
			Type = type;
		}
		
		public Event Clone()
		{
			Event evt = (Event)this.MemberwiseClone();
			evt.Actions = new List<Action>();
			foreach(Action act in Actions)
			{
				evt.Actions.Add(act.Clone());
			}
			return evt;
		}
	}
	
	public class Instance
	{
		Gamadus.Designer.Object obj
		{
			get
			{
				return (Object)ResourceMgr.Instance.GetResourceByID(id);
			}
		}
		int id;
		Point position;
		public Instance(Gamadus.Designer.Object obj, Point position) : this(obj.ID, position) {}
		
		public Instance(int id, Point position)
		{
			this.position = position;
			this.id = id;
		}
		
		public Point Position
		{
			get
			{
				return position;
			}
		}
		
		public int ObjectID
		{
			get
			{
				return id;
			}
		}
		public Instance Clone()
		{
			return (Instance)this.MemberwiseClone();
		}
	}
}